<template>
	<view class="content">
		<!-- <u-picker :show="showPicker" :columns="columns" closeOnClickOverlay  
		@close="handleHidePicker()" @cancel="handleHidePicker()" @confirm="handleConfirmPicker" keyName="name"
		></u-picker> -->
		<u-picker :show="showPeriod" :columns="periodColumns" closeOnClickOverlay @close="handleHidePeriod()"
			@cancel="handleHidePeriod()" @confirm="handleConfirmPeriod" keyName="name"></u-picker>
		<mescroll-uni ref="mescrollRef" @init="mescrollInit" @up="upCallback" :up="upOption" :down="{use: false}">
			<view class="section-0">
				<view class="section-body">
					<!-- <view class="top-tab">
						<view :class="`item ${curTab.id === item.id ? 'item__active' : ''}`" @click="setCurTab(item)"
							v-for="item in tabList" :key="item.id">
							{{item.name}}
							<view class="shape"></view>
						</view>
					</view> -->
					<view class="charts">
						<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts"
							class="echarts"></view>
					</view>
					<view class="form">
						<!-- <formArea :value="form.city" :labelWidth="134" @onChange="(e) => form.city = e"></formArea> -->
						<!-- 			<view class="form-item2">
							<view class="label">项目名称</view>
							<view class="shape"></view>
							<view class="input">
								<u--input
									placeholderClass="input-placeholder"
									class="input-body"
									v-model="form.project_name"
								  ></u--input>
							</view>
						</view> -->
						<view class="form-item2">
							<view class="label">规模</view>
							<view class="shape"></view>
							<view class="input">
								<u--input placeholderClass="input-placeholder" class="input-body"
									v-model="form.project_dy_level"></u--input>
							</view>
						</view>
						<view class="form-item2">
							<view class="label">建设程度</view>
							<view class="shape"></view>
							<view class="input">
								<view class="input-body-text" @click="handleShowPeriod">
									{{periodColumns[0].filter(item => item.type === form.project_period)[0].name}}
								</view>
							</view>
						</view>
						<view class="actions">
							<view class="action" @click="handleResetForm()">
								<text>重</text>
								<text>置</text>
							</view>
							<view class="action search" @click="handleSearch()">
								<text>查</text>
								<text>询</text>
							</view>
						</view>

					</view>

					<view class="space"></view>
					<view class="list">
						<view :class="`item ${item.show ? 'item__expand' : ''}`" v-for="(item, idx) in list"
							:key="item.id">
							<view class="parent" @click="expandChange(item.id)">
								<view class="action">
									<view class="iconfont icon-xiangyou1"></view>
								</view>
								<view class="name">
									{{item.code}}.{{item.project_name}}
								</view>
								<view class="info">
									<text>计划开工时间 {{item.kg_time && item.kg_time.substring(0,10)}}</text>
									<text>计划投产时间 {{item.tc_time && item.tc_time.substring(0,10)}}</text>
								</view>
							</view>
							<view class="child" v-if="item.show">
								<view class="child-item" v-for="(item2, index) in item.children" :key="item2.id">
									<view class="top">
										<view class="name">
											{{idx + 1}}-{{index + 1}}.{{item2.project_name}}
										</view>
										<view class="info">
											<text>计划投产时间：{{item2.tc_time && item2.tc_time.substring(0,10)}}</text>
											<text>总投资：{{item2.project_tz}}</text>
										</view>
										<view class="info">
											<text>实际投产时间：{{item2.tc_time_real && item2.tc_time_real.substring(0,10)}}</text>
											<text>规模：{{item2.project_type == 1 ? item2.project_scale_1 + '万千伏' : item2.project_scale_2 + '公里'}}</text>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>

				</view>
			</view>
		</mescroll-uni>
	</view>
</template>

<script>
	import {
		projectAllApi,
		projectBuildApi,
		projectSonApi
	} from '../../../api/statistics.js'
	import {
		projectCityApi,
	} from '@/api/project.js';
	// import formArea from '@/components/form_area/index.vue';
	export default {
		// components: {
		// 	formArea
		// },
		data() {
			return {
				showCharts: true,
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				upOption: {
					textNoMore: '-- 没有更多了 --'
				},
				pageSize: 20,
				companyName: '',
				projectName: '',
				curTab: {
					id: 2
				},
				option: {},
				// 地区、计划开工、计划投产、规模、建设程度（在建、未开、已投）
				tabList: [{
						name: '计划开工',
						id: 2
					},
					{
						name: '计划投产',
						id: 3
					},
					// {
					// 	name: '建设程度',
					// 	id: 5,
					// 	children: [
					// 		{
					// 			name: '全部',
					// 			id: 6,
					// 			type: ''
					// 		},
					// 		{
					// 			name: '在建',
					// 			id: 6,
					// 			type: '2'
					// 		},
					// 		{
					// 			name: '未开',
					// 			id: 7,
					// 			type: '5'
					// 		},
					// 		{
					// 			name: '已投',
					// 			id: 8,
					// 			type: '3'
					// 		},
					// 	]
					// },
				],
				list: [],
				// showPicker: false,
				showPeriod: false,
				columns: [],
				periodColumns: [
					[{
							id: 1,
							name: '全部',
							type: ''
						},
						{
							id: 1,
							name: '在建',
							type: '2'
						},
						{
							id: 1,
							name: '未开',
							type: '5'
						},
						{
							id: 1,
							name: '已投',
							type: '3'
						}
					]
				],
				form: {
					project_dy_level: '',
					city: '全部',
					project_period: ''
				},
				option: {
					grid: {
						left: '15px',
						right: '0',
						bottom: '0',
						top: '15px',
						containLabel: true
					},
					tooltip: {
						show: false
					},
					legend: {
						show: false
					},
					xAxis: {
						data: [],
						type: 'category',
						axisLine: {
							show: false
						},
						axisTick: {
							show: false
						},
						axisLabel: {
							interval: 0,
							rotate: 30,
							textStyle: {
								color: '#3b3c40',
								fontSize: 10
							}
						},
					},
					yAxis: {
						type: 'value',
						axisLine: {
							show: false
						},
						splitLine: {
							show: true,
							lineStyle: {
								color: '#d9d9d7',
								type: 'dashed'
							}
						},
						axisTick: {
							show: false
						},
						axisLabel: {
							show: false,
							textStyle: {
								color: '#3b3c40',
								fontSize: 12
							}
						},
					},
					series: [{
						name: '数量',
						type: 'bar',
						data: [],
						barWidth: '30%',
						itemStyle: {
							normal: {
								color: '#95c4fa',
								barBorderRadius: 12,
							},
						},
						label: {
							normal: {
								show: true,
								lineHeight: 10,
								formatter: '{c}',
								position: 'top',
								textStyle: {
									color: '#3b3c40',
									fontSize: 10,
								},
							},
						},
						// selectedMode: 'multiple',
						selectedMode: 'single',
						select: {
							itemStyle: {
								color: '#0535d6',
								barBorderRadius: 12,
							}
						},
					}]
				}
			}
		},
		onReady() {
			this.projectCity();
		},
		methods: {
			handleResetForm() {
				for (let key in this.form) {
					this.form[key] = '';
				}
				this.form.city = '全部'
				this.projectCity();
				this.mescroll.resetUpScroll();
this.mescroll.scrollTo(0,0);
			},
			handleSearch() {
				this.projectCity();
				this.mescroll.resetUpScroll();
this.mescroll.scrollTo(0,0);
			},
			// handleShowPicker() {
			// 	this.showPicker = true;
			// },
			// handleHidePicker() {
			// 	this.showPicker = false;
			// },
			// handleConfirmPicker(e) {
			// 	const { value } = e;
			// 	const item = value[0]; // {id: 7, name: '未开'}
			// 	this.handleHidePicker();
			// 	this.handleSearch();
			// },
			handleShowPeriod() {
				this.showPeriod = true;
			},
			handleHidePeriod() {
				this.showPeriod = false;
			},
			handleConfirmPeriod(e) {
				const {
					value
				} = e;
				const item = value[0]; // {id: 7, name: '未开'}
				this.form.project_period = item.type;
				this.handleHidePeriod();
			},
			onEchartSelect(res) {
				if (res.res.selected && res.res.selected.length) {
					let city = [];
					res.res.selected[0].dataIndex.forEach(item => {
						city.push(this.option.xAxis.data[item]);
					})
					this.form.city = city.join(',');
					this.mescroll.resetUpScroll();
this.mescroll.scrollTo(0,0);
				} else {
					this.form.city = '';
					this.mescroll.resetUpScroll();
this.mescroll.scrollTo(0,0);
				}
			},
			async projectCity() {
				uni.showLoading({
					title: '加载中...'
				})
				let obj = {
					...this.form,
				}
				if (this.form.city === '全部') {
					delete obj.city;
				}
				const {
					data
				} = await projectCityApi({
					...obj,
				});
				let nameArr = [];
				let dataArr = [];
				for (let key in data.data) {
					nameArr.push(key);
					dataArr.push(data.data[key]);
				}
				this.option.series[0].data = dataArr
				this.option.xAxis.data = nameArr
				uni.hideLoading()
			},
			/*mescroll组件初始化的回调,可获取到mescroll对象 (此处可删,mixins已默认)*/
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},

			/*上拉加载的回调*/
			async upCallback(page) {
				let obj = {
					...this.form,
				}
				if (this.form.city === '全部') {
					delete obj.city;
				}
				const {
					data
				} = await projectAllApi({
					...obj,
					type: 0,
					page: page.num,
					pageSize: this.pageSize
				});


				if (data.code === 1 || data.code === 200) {
					const list = [];
					for (let key in data.data.data) {
						list.push({
							...data.data.data[key],
							show: false
						})
					}

					// 接口返回的当前页数据列表 (数组)
					let curPageData = list;
					curPageData.forEach((item, idx) => {
						curPageData[idx].code = idx + 1 + (page.num - 1) * this.pageSize
					})
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length;
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					let totalPage = data.data.last_page;
					//设置列表数据
					if (page.num == 1) this.list = []; //如果是第一页需手动置空列表
					this.list = this.list.concat(curPageData); //追加新数据
					// 请求成功,隐藏加载状态
					//方法一(推荐): 后台接口有返回列表的总页数 totalPage
					this.mescroll.endByPage(curPageLen, totalPage);
					setTimeout(() => {
						this.mescroll.endSuccess(curPageLen)
					}, 20)
				} else {
					//  请求失败,隐藏加载状态
					this.mescroll.endErr()
				}
			},
			// async setCurTab(item) {
			// 	if (item.children && item.children.length !== 0) {
			// 		this.columns = [item.children];
			// 		this.handleShowPicker();
			// 	}
			// 	this.curTab = item;
			// 	if (this.dataType[item.id]) {
			// 		this.setDataType(this.dataType[item.id][0]);
			// 	}
			// },

			async expandChange(id) {
				const dep = JSON.parse(JSON.stringify(this.list));

				let _idx = undefined;
				dep.forEach((item, idx) => {
					if (item.id === id) {
						if (dep[idx].show) {
							dep[idx].show = false;
						} else {
							dep[idx].show = true;
							_idx = idx;
							if (!dep[idx].children) {
								dep[idx].children = [];
							}
						}
					}
				})
				if (_idx !== undefined && dep[_idx].children && dep[_idx].children.length === 0) {
					uni.showLoading({
						title: '加载中...'
					})
					const {
						data
					} = await projectSonApi({
						project_id: dep[_idx].id
					});
					uni.hideLoading();
					dep[_idx].children = data.data;
				}
				this.list = dep;
			},
		}
	}
</script>
<script module="echarts" lang="renderjs">
	let myChart
	let _ownerInstance
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				myChart = echarts.init(document.getElementById('echarts'))
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.option)
				myChart.on('selectchanged', 'series', function(res) {
					_ownerInstance.callMethod('onEchartSelect', {
						res,
					})
					// {
					// 	"type": "selectchanged",
					// 	"selected": [{
					// 		"dataIndex": [4],
					// 		"seriesIndex": 0
					// 	}],
					// 	"isFromClick": true,
					// 	"fromAction": "select",
					// 	"fromActionPayload": {
					// 		"type": "select",
					// 		"dataIndexInside": 4,
					// 		"seriesIndex": 0,
					// 		"isFromClick": true
					// 	}
					// }
				});
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				if (!_ownerInstance) {
					_ownerInstance = ownerInstance
				}
				// myChart.dispose();
				// this.initEcharts()
				
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		},
	}
</script>
<style lang="scss" scoped>
	.charts {
		width: 100%;
		box-sizing: border-box;
		height: 315rpx;
		padding: 0 70rpx;
		padding-left: calc(70rpx - 15px);
		margin-bottom: 30rpx;
		#echarts {
			width: 100%;
			height: 100%;
		}
	}

	.form {

		width: 100%;
		padding: 0 75rpx;
		padding-top: 0rpx;
		box-sizing: border-box;
		position: relative;

		.area-container {
			position: absolute;
			top: 110rpx;
			left: 75rpx;
			z-index: 10;
			width: 600rpx;
			background-color: #1433cd;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 24rpx;
			padding-bottom: 12rpx;

			&::before {
				position: absolute;
				top: -12rpx;
				left: 32rpx;
				content: '';
				border-bottom: 12rpx solid #1433cd;
				border-left: 12rpx solid transparent;
				;
				border-right: 12rpx solid transparent;
			}

			.item {
				padding: 0 18rpx;
				height: 44rpx;
				background-color: #fafbfd;
				border-radius: 20rpx;
				margin-bottom: 12rpx;
				color: #1433cd;
				font-size: 26rpx;
				text-align: center;
				line-height: 44rpx;
				float: left;
				margin-right: 12rpx;
			}

			.item__active {
				background-color: orange;
				color: #fff;
			}

		}

		.form-item2 {

			width: 600rpx;
			height: 48rpx;
			border-radius: 20rpx;
			border: solid 2rpx #1534ce;
			margin: 0 auto;
			margin-bottom: 34rpx;
			@include clear();

			.label {
				float: left;
				width: 134rpx;
				height: 48rpx;
				text-align: center;
				line-height: 44rpx;
				font-size: 26rpx;
				font-weight: normal;
				font-stretch: normal;
				letter-spacing: 0rpx;
				color: #1534ce;
			}

			.shape {
				width: 3rpx;
				height: 32rpx;
				background-color: #7f98ae;
				float: left;
				margin-top: 8rpx;
			}

			.input {
				float: left;
				width: calc(100% - 134rpx - 3rpx);

				.input-body {
					box-sizing: border-box;
					width: 100%;
					height: 44rpx;
					padding: 0 !important;
					border: none;
					padding-left: 30rpx !important;
					padding-right: 30rpx !important;
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: rgba(0, 0, 0, 0.7);
				}

				.input-body-text {
					box-sizing: border-box;
					width: calc(100% - 80rpx);
					height: 44rpx;
					line-height: 44rpx;
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #1534ce;
					text-align: center;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
				}

				.input-placeholder {
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: rgba(0, 0, 0, 0.5);
				}
			}

		}

		.form-item {
			width: 600rpx;
			height: 48rpx;
			border-radius: 20rpx;
			border: solid 2rpx #1534ce;
			margin: 0 auto;
			margin-bottom: 34rpx;
			@include clear();

			.label {
				float: left;
				width: 77rpx;
				height: 48rpx;
				text-align: center;
				line-height: 44rpx;
				font-size: 26rpx;
				font-weight: normal;
				font-stretch: normal;
				letter-spacing: 0rpx;
				color: #1534ce;
			}

			.shape {
				width: 3rpx;
				height: 32rpx;
				background-color: #7f98ae;
				float: left;
				margin-top: 8rpx;
			}

			.input {
				float: left;
				width: calc(100% - 77rpx - 80rpx - 3rpx);

				.input-body {
					box-sizing: border-box;
					width: 100%;
					height: 44rpx;
					line-height: 44rpx;
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #1534ce;
					text-align: center;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
				}
			}

			.action {
				float: left;
				width: 80rpx;
				line-height: 44rpx;
				height: 44rpx;
				text-align: center;
				font-size: 32rpx;
				color: #1534ce;
				transition: 0.3s ease;
			}

			.action__active {
				transform: rotate(180deg);
			}
		}

		.size2 {
			.label {
				width: 77rpx;
			}

			.input {
				width: calc(100% - 77rpx - 3rpx);
				position: relative;

				.iconfont {
					position: absolute;
					right: 25rpx;
					top: 7rpx;
					font-size: 30rpx;
					color: #dad9df;
				}
			}
		}


	}

	.section-0 {
		width: 100%;
		background: #2e5efe;

		.section-body {
			width: 100%;
			height: 100%;
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
			background-color: #fff;
			overflow: hidden;
			padding-top: 20rpx;

			// .space {
			// 	width: 100%;
			// 	height: 22rpx;

			// 	background-color: #fafbfd;
			// }

			.list {
				width: 100%;
				box-sizing: border-box;
				padding: 0 40rpx;
				box-shadow: 0rpx 10rpx 20rpx 0rpx rgba(0, 0, 0, 0.05) inset;

				.item {
					width: 100%;
					border-bottom: solid 2rpx #f2f2f0;

					.parent {
						width: 100%;
						padding: 20rpx 0;
						@include clear();

						.info {
							width: 100%;
							float: left;
							height: 24rpx;
							line-height: 24prx;
							font-size: 16rpx;
							color: #7f98ae;
							margin-top: 8rpx;

							text {
								margin-right: 30rpx;

								&:last-child {
									margin-right: 0;
								}
							}
						}

						.name {
							width: calc(100% - 50rpx);
							height: 40rpx;
							font-size: 26rpx;
							font-weight: normal;
							font-stretch: normal;
							line-height: 40rpx;
							letter-spacing: 2rpx;
							color: #3b3c40;
							text-overflow: ellipsis;
							overflow: hidden;
							white-space: nowrap;
							float: left;
						}

						.action {
							float: left;
							width: 32rpx;
							margin-right: 18rpx;
							height: 100%;
							line-height: 45rpx;
							text-align: left;
							color: #3b3c40;

							.iconfont {
								font-size: 32rpx;
								transition: 0.2s ease;
								transform: rotate(90deg);
							}

						}
					}

					.child {
						width: 750rpx;
						margin-left: - 40rpx;
						background: #f2f2fa;

						.child-item {
							width: 620rpx;
							margin-left: 90rpx;
							border-bottom: 2rpx solid #e3e8f5;

							&:last-child {
								border-bottom: none;
							}

							.top {
								width: 100%;
								overflow: hidden;
								padding-top: 20rpx;
								padding-bottom: 10rpx;

								.name {
									float: left;
									width: 620rpx;
									height: 50rpx;
									line-height: 50rpx;
									color: #3b3c40;
									font-size: 26rpx;
									text-overflow: ellipsis;
									overflow: hidden;
									white-space: nowrap;
								}

								.info {
									width: 100%;
									float: left;
									height: 24rpx;
									line-height: 24prx;
									font-size: 16rpx;
									color: #7f98ae;
									margin-bottom: 8rpx;

									text {
										margin-right: 30rpx;

										&:last-child {
											margin-right: 0;
										}
									}
								}
							}
						}
					}
				}

				.item__expand {
					.parent {
						.action {
							.iconfont {
								transform: rotate(-90deg);
							}
						}
					}

					.child {
						box-shadow: 0rpx 10rpx 10rpx 0rpx rgba(0, 0, 0, 0.03) inset;
					}
				}
			}

			.actions {
				width: 100%;
				height: 50rpx;
				margin-top: 29rpx;
				padding-bottom: 40rpx;
				display: flex;
				justify-content: space-between;

				.action {
					width: 288rpx;
					height: 48rpx;
					background-color: #395cf5;
					border-radius: 20rpx;
					text-align: center;
					line-height: 48rpx;
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					line-height: 48rpx;
					color: #ffffff;

					text {
						&:first-child {
							margin-right: 20rpx;
						}
					}
				}

				.search {
					background-color: #1534ce;
				}
			}

			.total {
				box-sizing: border-box;
				padding: 0 70rpx;
				height: 80rpx;
				@include clear();

				.data-type {
					float: left;
					height: 80rpx;
					line-height: 80rpx;

					.data-type-item {
						float: left;
						position: relative;
						padding-right: 15rpx;
						padding-left: 15rpx;
						font-size: 22rpx;
						font-weight: normal;
						font-stretch: normal;
						line-height: 80rpx;
						letter-spacing: 0rpx;
						color: #d8d8d5;
						transition: 0.3s ease;

						&:first-child {
							padding-left: 0;
						}

						&:last-child {
							&:before {
								display: none;
							}
						}

						&:before {
							content: '';
							width: 2rpx;
							height: 20rpx;
							background-color: #d8d8d5;
							position: absolute;
							right: -1rpx;
							top: 30rpx;
						}
					}

					.data-type-item__active {
						color: #3b3c40;
					}
				}

				.total-count {
					font-size: 22rpx;
					font-weight: normal;
					font-stretch: normal;
					line-height: 80rpx;
					letter-spacing: 0rpx;
					color: #3b3c40;
					float: right;
				}
			}

			// .charts {
			// 	width: 100%;
			// 	box-sizing: border-box;
			// 	height: 270rpx;
			// 	padding: 0 70rpx;
			// 	box-shadow: 0 10rpx 10rpx 5rpx rgba(#bcbcb5, 0.17);
			// 	position: relative;
			// 	z-index: 10;
			// 	padding-bottom: 30rpx;

			// 	#echarts {
			// 		width: 100%;
			// 		height: 100%;
			// 	}
			// }

			.top-tab {
				width: 670rpx;
				margin-left: 40rpx;
				height: 52rpx;
				line-height: 52rpx;
				display: flex;
				justify-content: space-between;
				box-sizing: border-box;
				padding: 0 30rpx;
				margin-top: 15rpx;
				border-bottom: solid 3rpx #f2f2f0;

				.item {
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					line-height: 50rpx;
					letter-spacing: 0rpx;
					color: #d8d8d5;
					position: relative;

					.shape {
						opacity: 0;
						transform: translateY(6rpx);
						position: absolute;
						bottom: -6rpx;
						width: 100%;
						height: 8rpx;
						background-color: #1534ce;
						border-radius: 8rpx;
						transition: 0.3s ease;
					}
				}

				.item__active {
					color: #3b3c40;

					.shape {
						opacity: 1;
						transform: translateY(0rpx);
					}
				}
			}
		}
	}
</style>
